<!--
 * @Description: 面料信息
 *
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-07-03 17:11:54
 * @FilePath: /yuxiuproweb/src/views/money/log/index.vue
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="存货编码" prop="number">
        <el-input
          v-model="queryParams.number"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存货名称" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          style="width: 100%"
        >
          <el-option key="1" label="窗帘" value="1" />
          <el-option key="2" label="墙布" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否备货" prop="isStockUp">
        <el-select v-model="queryParams.isStockUp" style="width: 100%">
          <el-option key="1" label="是" value="1" />
          <el-option key="0" label="否" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否采购" prop="isbuy">
        <el-select v-model="queryParams.isbuy" style="width: 100%">
          <el-option key="1" label="是" :value="1" />
          <el-option key="0" label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-select
          v-model="queryParams.vendorName"
          style="width: 100%"
          filterable
          remote
          reserve-keyword
          :remote-method="getSupplierData"
          clearable
        >
          <el-option
            v-for="item in supplierData"
            :key="item.vendorId"
            :label="item.vendorName"
            :value="item.vendorName"
          >
            <span style="padding-left: 6px">{{ item.vendorName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商类型" prop="vendorModel" label-width="120px">
        <el-input
          v-model="queryParams.vendorModel"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pro:record:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
	    <el-button
	      type="info"
	      plain
	      icon="el-icon-upload2"
	      size="mini"
	      @click="handleImport"
	      v-hasPermi="['system:user:import']"
	      >导入</el-button
	    >
	  </el-col> -->

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pro:record:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pro:record:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div style="float: left; width: 80%">
      <el-table
        v-loading="loading"
        :data="recordList"
        @row-click="getmlinfoList"
        @selection-change="handleSelectionChange"
        :row-class-name="tableRowClassName"
        :max-height="maxHeight"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="存货编码" align="center" prop="number" />
        <el-table-column label="存货名称" align="center" prop="name" />
          <el-table-column label="供应商" align="center" prop="vendorName" />
            <el-table-column label="供应商类型" align="center" prop="vendorModel" />
        <el-table-column
          label="实际库存"
          align="center"
          prop="inventoryQuantity"
        />
        <el-table-column
          label="最高库存"
          align="center"
          prop="maximumInventory"
        />
        <el-table-column
          label="最低库存"
          align="center"
          prop="minimumInventory"
        />
        <el-table-column
          label="安全库存"
          align="center"
          prop="safetyInventory"
        />
        <el-table-column label="类型" align="center" prop="type">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">窗帘</span>
            <span v-else>墙布</span>
          </template>
        </el-table-column>
        <el-table-column label="是否备货" align="center" prop="isStockUp">
          <template slot-scope="scope">
            <span v-if="scope.row.isStockUp == 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column label="是否采购" align="center" prop="isStockUp">
          <template slot-scope="scope">
            <span v-if="scope.row.isbuy == 1">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope" v-if="scope.row.isprocess != 1">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['pro:record:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['pro:record:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div style="float: left; width: 20%">
      <div>
        <el-table v-loading="loading" :data="mlinfo" :max-height="maxHeight">
          <!-- 	<el-table-column type="index" label="序号" width="60"></el-table-column> -->
          <el-table-column label="存货条码" prop="barcode"></el-table-column>
          <el-table-column label="存货名称" prop="frbaicName"></el-table-column>
          <el-table-column
            label="库存量"
            prop="inventoryQuantity"
          ></el-table-column>
        </el-table>
      </div>
      <div>
        <el-table
          v-loading="loading"
          :data="mlorderinfo"
          :max-height="maxHeight"
        >
          <!-- 	<el-table-column type="index" label="序号" width="60"></el-table-column> -->
          <el-table-column label="客户" prop="clientName"></el-table-column>
          <el-table-column label="工单号" prop="workOrderNo"></el-table-column>
          <el-table-column label="使用量" prop="number"></el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 添加或修改交易记录对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="存货编码" prop="number">
          <el-input v-model="form.number" placeholder="请输入存货编码" />
        </el-form-item>
        <el-form-item label="存货名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入存货名称" />
        </el-form-item>
        <el-form-item label="最高库存" prop="maximumInventory">
          <el-input
            v-model="form.maximumInventory"
            placeholder="请输入最高库存"
          />
        </el-form-item>
        <el-form-item label="最低库存" prop="minimumInventory">
          <el-input
            v-model="form.minimumInventory"
            placeholder="请输入最低库存"
          />
        </el-form-item>
        <el-form-item label="安全库存" prop="safetyInventory">
          <el-input
            v-model="form.safetyInventory"
            placeholder="请输入安全库存"
          />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select
            style="width: 100%"
            v-model="form.type"
            filterable
            placeholder="请选择类型"
          >
            <el-option key="1" label="窗帘" value="1"></el-option>
            <el-option key="2" label="墙布" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否备货" prop="isStockUp">
          <el-select
            style="width: 100%"
            v-model="form.isStockUp"
            filterable
            placeholder="请选择是否备货"
          >
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="0" label="否" value="0"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否采购" prop="isbuy">
          <el-select v-model="form.isbuy" style="width: 100%">
            <el-option key="1" label="是" :value="1" />
            <el-option key="0" label="否" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="safetyInventory">
          <!-- <el-input v-model="form.vendorName" placeholder="请输入供应商" /> -->
          <el-select
            v-model="form.vendorId"
            placeholder="请选择供应商名称"
            style="width: 100%"
            filterable
            remote
            reserve-keyword
            :remote-method="getSupplierData"
            @change="changeVendor"
          >
            <el-option
              v-for="item in supplierData"
              :key="item.vendorId"
              :label="item.vendorName"
              :value="item.vendorId"
            >
              <span style="padding-left: 6px">{{ item.vendorName }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商型号" prop="vendorModel">
          <el-input v-model="form.vendorModel" placeholder="请输入供应商型号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 收入导入对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <el-link
	        type="primary"
	        :underline="false"
	        style="font-size: 12px; vertical-align: baseline"
	        @click="importTemplate"
	        >下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getProductByFabricName,
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateProcess,
  updateRecord,
} from "@/api/material/fabricparms";
import { listInventory } from "@/api/material/inventory";
import { getToken } from "@/utils/auth";
import commonMix from "@/utils/mixins/common";
import { listVendor } from "@/api/mes/md/vendor";
export default {
  mixins: [commonMix],
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      isprocess: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //面料库存详细情况
      mlinfo: [],
      //面料对应未裁切订单
      mlorderinfo: [],
      // 交易记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        number: null,
        name: null,
        type: null,
        vendorName: null,
        vendorModel: null,
      },
      // 收入导入参数
      upload: {
        // 是否显示弹出层（收入导入）
        open: false,
        // 弹出层标题（收入导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的收入数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData",
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      supplierData: [], //获取供应商信息
    };
  },
  created() {
    this.getList();
    this.getSupplierData();
  },
  methods: {
    tableRowClassName({ row }) {
      if (row.safetyInventory * 100 > row.inventoryQuantity * 100) {
        return "success-row";
      }
      return "";
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    // importTemplate() {
    //   this.download(
    //     "system/user/importTemplate",
    //     {},
    //     `user_template_${new Date().getTime()}.xlsx`
    //   );
    // },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "收入导入";
      this.upload.open = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    /** 查询交易记录列表 */
    getList() {
      this.loading = true;
      const params = {
        ...this.queryParams,
        startDate: this.queryParams.processTimeObj?.[0] || null,
        endDate: this.queryParams.processTimeObj?.[1] || null,
      };
      delete params.processTimeObj;
      listRecord(params).then((response) => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getmlinfoList(row, event, column) {
      // console.log(11111)
      // console.log(row)
      // this.loading = true;
      listInventory({
        frbaicName: row.name,
      }).then((response) => {
        this.mlinfo = response.rows;
      });
      getProductByFabricName({
        fabricName: row.name,
      }).then((response) => {
        this.mlorderinfo = response.data;
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        number: null,
        name: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.transactionRecordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加面料";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inventorySettingId = row.inventorySettingId || this.ids;
      getRecord(inventorySettingId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改面料设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.inventorySettingId != null) {
            updateRecord(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.form.orderId == null) {
              this.form.orderId = this.parseTime(new Date(), "{y}{m}{d}");
            }
            console.log(this.form);
            addRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      const inventorySettingId = row.inventorySettingId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除面料设置记录编号为"' + inventorySettingId + '"的数据项？'
        )
        .then(function () {
          return delRecord(inventorySettingId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pro/transactionRecord/export",
        {
          ...this.queryParams,
        },
        `record_${new Date().getTime()}.xlsx`
      );
    },
    getSupplierData(vendorName = "") {
      listVendor({
        pageNum: 1,
        pageSize: 100,
        vendorName,
      }).then(({ rows }) => {
        this.supplierData = rows;
      });
    },
    changeVendor(val) {
      var index = this.supplierData.findIndex((a) => a.vendorName == val);
      if (index != -1) {
        this.form.vendorId = this.supplierData[index].vendorId;
        this.form.vendorName = this.supplierData[index].vendorName;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep {
  .el-table .success-row {
    background: #fbc4c4;
  }
}
</style>
